﻿System.useCodepage = true;

#include "Court_LoadSWFMenu.as"
#include "Court_PublicFunctions.as"

var _xml = xml;
//_xml = "test.xml";//only for testing
var xml_file = new XML(); 
var partNum = 0;
var loadedPart = 0;
var partWithRelation = "";
var appliedPart = "";
var applydAd = "";
var currentButtonNum = 0;


xml_file.ignoreWhite = true;
xml_file.onLoad = function (success)
{
    if (!success){
		trace("unsucceed");
		showMsg(_root, "对不起! XML加载错误！请检查文件及路径！", 240, 150, 430, 200, 0xFF0000);
		return;
	}
	createEnvironment();
};

xml_file.load(_xml);

function createEnvironment(environmentXml){
	if (xml_file.childNodes[0].nodeName=="Environment"){
		environmentXml = xml_file.childNodes[0];
		for (z=0;z<environmentXml.childNodes.length;z++){
			displayOrder = environmentXml.childNodes[z].attributes.displayOrder!=undefined? environmentXml.childNodes[z].attributes.displayOrder:0;
			part_mc = mainScreen_mc.createEmptyMovieClip("Main"+ environmentXml.childNodes[z].nodeName +"_mc",displayOrder);
			part_mc.cname = environmentXml.childNodes[z].nodeName;
			part_mc._x = Number(environmentXml.childNodes[z].attributes.posX);
			part_mc._y = Number(environmentXml.childNodes[z].attributes.posY);
			part_mc.eWidth = Number(environmentXml.childNodes[z].attributes.eWidth);
			part_mc.eHeight = Number(environmentXml.childNodes[z].attributes.eHeight);

			part_mc.imgUrl = environmentXml.childNodes[z].attributes.imgUrl;
			part_mc.createEmptyMovieClip("background_mc", -1);
			
			part_mc.createEmptyMovieClip("content_mc", part_mc.getNextHighestDepth());
			part_mc.content_mc._x = environmentXml.childNodes[z].attributes.screenX!=undefined? Number(environmentXml.childNodes[z].attributes.screenX):0;
			part_mc.content_mc._y = environmentXml.childNodes[z].attributes.screenY!=undefined? Number(environmentXml.childNodes[z].attributes.screenY):0;
			part_mc.content_mc.elementWidth = Number(environmentXml.childNodes[z].attributes.elementWidth);
			part_mc.content_mc.elementHeight = Number(environmentXml.childNodes[z].attributes.elementHeight);
			part_mc.content_mc.elementRepeat = environmentXml.childNodes[z].attributes.elementRepeat;
			part_mc.content_mc.elementGap = environmentXml.childNodes[z].attributes.elementGap!=undefined? Number(environmentXml.childNodes[z].attributes.elementGap):0;
			part_mc.content_mc.elementPerScreen = environmentXml.childNodes[z].attributes.elementPerScreen!=undefined? Number(environmentXml.childNodes[z].attributes.elementPerScreen):0;
			part_mc.content_mc.arrowImg = environmentXml.childNodes[z].attributes.arrowImg;
			part_mc.content_mc.applyImg = environmentXml.childNodes[z].attributes.applyImg;
			part_mc.content_mc.scrollSpeed = environmentXml.childNodes[z].attributes.scrollSpeed!=undefined? Number(environmentXml.childNodes[z].attributes.scrollSpeed):0;

			part_mc.content_mc.scrollerIsShow = false;
			
			//part_mc.createEmptyMovieClip("loading_mc", part_mc.getNextHighestDepth());
			if (part_mc.imgUrl != undefined)
				loadPic(part_mc.imgUrl, part_mc.background_mc);
			if (part_mc.cname == "SaveButton")
			{
				loadSaveButton(part_mc);
			}
			else if (part_mc.cname == "ImportSwf"){
				importSwf(part_mc);
			}
		}
		loadPart(mainScreen_mc, "Part");
	}
}

function loadPart(parent_mc, loadName){
	//trace("loadPart");
	for (i=1;i<xml_file.childNodes.length;i++){
		if (xml_file.childNodes[i].nodeName==loadName){
			partNum++;
		}
	}
	for (i=1;i<=partNum;i++)
		createPart(parent_mc, xml_file.childNodes[i],i);
}

function createPart(parent_mc, partXml,i){
	var cOrder = partXml.attributes.displayOrder!= undefined? partXml.attributes.displayOrder: 0;
	Part_mc = parent_mc["Main"+ partXml.nodeName +"_mc"]["content_mc"].createEmptyMovieClip("Part_"+ partXml.attributes.id, cOrder);
	Part_mc.id = partXml.attributes.id;
	Part_mc.cname = partXml.attributes.name;
	Part_mc.isShow = partXml.attributes.isShow;
	Part_mc.imgUrl = partXml.attributes.imgUrl;
	Part_mc.posX = partXml.attributes.posX;
	Part_mc.posY = partXml.attributes.posY;
	Part_mc.relatedPart = partXml.attributes.relatedPart;
	Part_mc.top = partXml.attributes.top;
	Part_mc.left = partXml.attributes.left;
	Part_mc._x = (partXml.attributes.posX!="relative")? partXml.attributes.posX: 0;
	Part_mc._y = (partXml.attributes.posY!="relative")? partXml.attributes.posY: 0;
	Part_mc.xml = partXml;
	if (partXml.attributes.isShow == "1" && (partXml.attributes.posX=="relative" || partXml.attributes.posY=="relative")){
		partWithRelation += Part_mc._name + ",";
	}
	if (partXml.childNodes[0].nodeName=="Button"){
		createButton(parent_mc["MainButton_mc"].content_mc, partXml.childNodes[0],Part_mc);
	}
	loadedPart++;
	if (loadedPart == partNum){
		resetPositionForAllRelatedPart(parent_mc["Main"+ partXml.nodeName +"_mc"]["content_mc"]);
	}
	if (Part_mc.isShow == "1")
		activePart(Part_mc);
	else
		inactivePart(Part_mc);
}

function createButton(parent_mc, buttonXml, relatedPart_mc){
	Button_mc = parent_mc.createEmptyMovieClip("Button_"+ relatedPart_mc.id, parent_mc.getNextHighestDepth());
	relatedPart_mc.button_mc = Button_mc;
	Button_mc.relatedPart_mc = relatedPart_mc;
	Button_mc.imgUrl = buttonXml.attributes.imgUrl;
	Button_mc.displayName = buttonXml.attributes.displayName;
	newButton(parent_mc, Button_mc,currentButtonNum, parent_mc.elementRepeat, parent_mc.elementWidth, parent_mc.elementHeight, parent_mc.elementGap, parent_mc.elementPerScreen);
	currentButtonNum++;
	Button_mc.onRelease = function(){
		if (!ifApply(this.relatedPart_mc.id)){
			activePart(this.relatedPart_mc);
			//trace(ifApply(this.relatedPart_mc.id));
		}
		else{
			inactivePart(this.relatedPart_mc);
		}
	}
}
function activePart(relatedPart_mc){
	//trace(relatedPart_mc.button_mc);
	if (relatedPart_mc.button_mc!=undefined && relatedPart_mc.button_mc._parent.applyImg!= undefined){
		apply_mc = relatedPart_mc.button_mc.apply_mc;
		if (apply_mc == undefined){
			apply_mc = relatedPart_mc.button_mc.createEmptyMovieClip("apply_mc",0);
			loadPic(relatedPart_mc.button_mc._parent.applyImg, apply_mc);
		}
		else
			slowShowMC(apply_mc);
	}
	loadPartPic(relatedPart_mc);
	loadOptionList(relatedPart_mc);
}
function inactivePart(relatedPart_mc){
	if (relatedPart_mc.button_mc!=undefined)
		slowHideMC(relatedPart_mc.button_mc.apply_mc);
		//relatedPart_mc.button_mc.apply_mc._x = -10000;
	unloadPartPic(relatedPart_mc);
}
function activeButton(relatedPart_mc){
	slowShowMC(relatedPart_mc);
	if (relatedPart_mc.button_mc!=undefined){
		relatedPart_mc.button_mc._alpha = 100;
		relatedPart_mc.button_mc.enabled = true;
	}
}
function inactiveButton(relatedPart_mc){
	//relatedPart_mc.isShow = "0";
	slowHideMC(relatedPart_mc);
	//removeApply(relatedPart_mc.id);
	if (relatedPart_mc.button_mc!=undefined){
		relatedPart_mc.button_mc._alpha = 50;
		relatedPart_mc.button_mc.enabled = false;
	}
}
function loadPartPic(_mc){
	_mc.isShow = "1";
	if (_mc.pic_mc==undefined){
		_mc.createEmptyMovieClip("pic_mc",0);
		loadPic(_mc.imgUrl, _mc.pic_mc);
	}
	slowShowMC(_mc);
	addApply(_mc);
	if (_mc.posX=="relative" || _mc.posY=="relative"){
		if (partWithRelation.indexOf(_mc._name)<0)
			partWithRelation += _mc._name + ",";
		resetPositionForPart(_mc);
	}
	else
		resetPositionForAllRelatedPart(_mc._parent);
}
function unloadPartPic(_mc){
	_mc.isShow = "0";
	slowHideMC(_mc);
	removeApply(_mc.id);
	if (_mc.posX=="relative" || _mc.posY=="relative")
		partWithRelation = partWithRelation.split(_mc._name + ",").join("");
	else
		resetPositionForAllRelatedPart(_mc._parent);
}

function resetPositionForAllRelatedPart(parentMc){
	//trace(partWithRelation);//
	
	partWithRelationArr = partWithRelation.split(",");
	for (m=0;m<partWithRelationArr.length-1;m++){
		resetPositionForPart(parentMc[partWithRelationArr[m]]);
	}
}
function resetPositionForPart(partMc){
	if (partMc.isShow == "1"){
		relatedPartArr = partMc.relatedPart.split(",");
		var minPositionX = minPositionY = 10000;
		var maxPositionX = maxPositionY = -10000;
		parentMc = partMc._parent;
		for(n=0;n<relatedPartArr.length;n++)
		{
			//trace(parentMc["Part_"+ relatedPartArr[n]].isShow);
			if (parentMc["Part_"+ relatedPartArr[n]].isShow == "1"){
				minPositionX = Math.min(minPositionX, parentMc["Part_"+ relatedPartArr[n]]._x);
				minPositionY = Math.min(minPositionY, parentMc["Part_"+ relatedPartArr[n]]._y);
				maxPositionX = Math.max(maxPositionX, parentMc["Part_"+ relatedPartArr[n]]._x);
				maxPositionY = Math.max(maxPositionY, parentMc["Part_"+ relatedPartArr[n]]._y);
			}
		}
		if (minPositionX==10000 || minPositionY == 10000 || maxPositionX == -10000 || maxPositionY == -10000){
			//trace("inactive");
			inactiveButton(partMc);
			return;
		}
		activeButton(partMc);
		if (partMc.posX=="relative"){
			if (partMc.left!= undefined)
				partMc._x = Number(partMc.left) + minPositionX;
			if (partMc.right!= undefined)
				partMc._x = Number(partMc.right) + maxPositionX;
		}
		else if (partMc.posY=="relative"){
			if (partMc.top!= undefined)
				partMc._y = Number(partMc.top) + minPositionY;
			if (partMc.bottom!= undefined)
				partMc._y = Number(partMc.bottom) + maxPositionY;
		}
	}
}

//Option Functions
function loadOptionList(relatedPart_mc){
	for (j=1;j<relatedPart_mc.xml.childNodes.length;j++){
		trace("loadOptionList_" + j);
		if (relatedPart_mc.xml.childNodes[j].nodeName == "OptionList")
			initAppliedAd(relatedPart_mc, j);
			createOptionList(relatedPart_mc.xml.childNodes[j],j, relatedPart_mc);
	}
}

function createOptionList(optionXml, id, relatedPart_mc){
	elementWidth = Number(optionXml.attributes.elementWidth);
	elementHeight = Number(optionXml.attributes.elementHeight);
	optionBox = relatedPart_mc.createEmptyMovieClip("optionBox_mc_" + id, id);
	optionBox.id = id;
	optionBox.valueId = 0;
	optionBox.valueName = "";
	optionBox._x = optionXml.attributes.left;
	optionBox._y = optionXml.attributes.top;
	optionValue_mc = optionBox.createEmptyMovieClip("optionValue_mc", optionBox.getNextHighestDepth());
	optionBg_mc = optionValue_mc.createEmptyMovieClip("bg_mc",optionValue_mc.getNextHighestDepth());
	drawRoundedRectangle(optionBg_mc, elementWidth, elementHeight, 0, 0xffffff, 20, false, false);
	optionPic_mc = optionValue_mc.createEmptyMovieClip("pic_mc",optionValue_mc.getNextHighestDepth());
	
	optionValue_mc.onRelease = function(){
		showOptionList(this._parent.listPanel_mc);
	}
	optionValue_mc.onRollOver = function(){
		if (this._parent.valueId != undefined)
			showTip(this._parent.valueName + ", 点击选择更多>", this, stageX(this) , stageY(this) - 20);
	}
	optionValue_mc.onRollOut = function(){
		if (this._parent.valueId != undefined)
			hiddenTip();
	}
	//create option list panel
	listPanel_mc = optionBox.attachMovie("Panel_mc","listPanel_mc",optionBox.getNextHighestDepth());
	listPanel_mc.posX = listPanel_mc._x = optionXml.attributes.listLeft;
	listPanel_mc._y = optionXml.attributes.listTop;
	listPanel_mc.elementWidth = elementWidth;
	listPanel_mc.elementHeight= elementHeight;
	listPanel_mc.elementRepeat= optionXml.attributes.elementRepeat;
	listPanel_mc.elementGap= Number(optionXml.attributes.elementGap);
	listPanel_mc.elementPerScreen= Number(optionXml.attributes.elementPerScreen);
	listPanel_mc.arrowImg= optionXml.attributes.arrowImg;
	listPanel_mc.scrollSpeed= Number(optionXml.attributes.scrollSpeed);
	listPanel_mc.totalNum = optionXml.childNodes.length;
	listPanel_mc.title_txt.text = optionXml.attributes.title;
	listPanel_mc.listWidth = optionXml.attributes.listWidth;
	listPanel_mc.listHeight = optionXml.attributes.listHeight;
	
	if (listPanel_mc.listHeight == "auto"){
		if (listPanel_mc.totalNum > listPanel_mc.elementPerScreen)
			panelHeight = listPanel_mc.elementPerScreen * (listPanel_mc.elementHeight + listPanel_mc.elementGap) + listPanel_mc.titleBg_mc._height + 10;
		else
			panelHeight = listPanel_mc.totalNum * (listPanel_mc.elementHeight + listPanel_mc.elementGap) + listPanel_mc.titleBg_mc._height + 10;
	}
	else{
		panelHeight = listPanel_mc.listHeight;
	}
	if (listPanel_mc.listWidth == "auto")
		panelWidth = listPanel_mc.elementWidth + 30 + listPanel_mc.close_btn._width;
	else
		panelWidth = listPanel_mc.listWidth;
	//trace(panelHeight + "," + panelWidth);
	listPanel_mc.titleBg_mc._width = panelWidth;
	listPanel_mc.title_txt._width = panelWidth - listPanel_mc.close_btn._width;
	listPanel_mc.close_btn._x = panelWidth - listPanel_mc.close_btn._width + 5;
	listPanel_mc.style_btn._x = panelWidth/2 - listPanel_mc.elementWidth/2;
	listPanel_mc.close_btn.onRelease = function(){
		hideOptionList(this._parent);
	}
	drawRoundedRectangle(listPanel_mc.Bg_mc, panelWidth, panelHeight, 7, 0x212429, 50);

	hideOptionList(listPanel_mc);
	
	for (z=0;z<listPanel_mc.totalNum;z++){
		createListButton(listPanel_mc, optionXml.childNodes[z], optionValue_mc, z);
	}
}
function createListButton(parent_mc, buttonXml, targetPart_mc, num){
	Button_mc = parent_mc.style_btn.createEmptyMovieClip("Button_"+ num, parent_mc.style_btn.getNextHighestDepth());
	Button_mc.imgUrl = buttonXml.attributes.imgUrl;
	Button_mc.displayName = buttonXml.attributes.displayName;
	Button_mc.isDefault = buttonXml.attributes.isDefault;
	Button_mc.valueId = buttonXml.attributes.valueId;
	Button_mc.valueImgUrl = buttonXml.attributes.valueImgUrl;
	newButton(parent_mc.style_btn, Button_mc, num, parent_mc.elementRepeat, parent_mc.elementWidth, parent_mc.elementHeight, parent_mc.elementGap, parent_mc.elementPerScreen);
	
	if (Button_mc.isDefault =="true" ){
		activeOption(Button_mc, targetPart_mc);
	}
	Button_mc.onRelease = function(){
		if (targetPart_mc._parent.valueId!= this.valueId)
			activeOption(this, targetPart_mc);
	}
}
function activeOption(button_mc, target_mc){
	target_mc.pic_mc.unloadClip();
	loadPic(button_mc.valueImgUrl, target_mc.pic_mc);
	var oldValueId = target_mc._parent.valueId;
	target_mc._parent.valueId = button_mc.valueId;
	target_mc._parent.valueName = button_mc.displayName;
	editAppliedAd(target_mc._parent, oldValueId);
	hideOptionList(button_mc._parent._parent);
}
function showOptionList(target_mc){
	//trace(target_mc);
	target_mc._x = target_mc.posX;
	slowShowMC(target_mc);
}
function hideOptionList(target_mc){
	//trace(target_mc);
	target_mc._x = 10000;
	slowHideMC(target_mc);
}
function initAppliedAd(partMc, order){
	//trace(partMc + "," + order);
	applydAd += partMc.id + "|" + order + "|" + 0 + ",";
}
function editAppliedAd(optionBox,oldAd){
	//trace(optionBox.valueId);
	oldAdStr = optionBox._parent.id + "|" + optionBox.id + "|" + oldAd;
	newAdStr = optionBox._parent.id + "|" + optionBox.id + "|" + optionBox.valueId;
	applydAd = applydAd.split(oldAdStr).join(newAdStr);
	trace(applydAd);
}
function loadSaveButton(saveButton_mc){
	save_btn = saveButton_mc.content_mc.attachMovie("Save_btn","Save_btn", saveButton_mc.content_mc.getNextHighestDepth);
	save_btn.onRelease = function(){
		var result_lv:LoadVars = new LoadVars();
		result_lv.onLoad = function(success:Boolean) {
			if (success) {
				//trace(result_lv.tipMessage);
				//result_ta.text = result_lv.tipMessage;
				//showMsg(_root,result_lv.tipMessage , 240, 0, 430, 200, 0xFF0000);
				
				showTip(result_lv.tipMessage,save_btn, stageX(save_btn)-50 , stageY(save_btn) - 40)
			} else {
				result_ta.text = "";
			}
		};
		var send_lv:LoadVars = new LoadVars();
		send_lv.appliedPart = appliedPart;//appliedPart
		
		adArr = applydAd.split(",");
		applydAdOutput = "";
		//trace(appliedPart);
		for (o=0;o<adArr.length-1;o++){
			trace(adArr[o]);
			adSubArr = adArr[o].split("|");
			applydAdOutput += adSubArr[0] + "|" + adSubArr[2] + ",";
		}
		//trace(applydAd);
		send_lv.ad = applydAdOutput;
		send_lv.sendAndLoad("receiveTest.aspx", result_lv, "POST");
		//send_lv.send("receiveTest.asp", "_blank", "POST");
	}
}
function importSwf(importSwf_mc){
	loadPic(importSwf_mc.imgUrl, importSwf_mc.content_mc);
}